import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/res_partner_odoo_api.dart';
import 'package:flutter_admin/models/odoo/res_partner.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/utils/utils.dart';
import '../../../generated/l10n.dart';

class ResPurchaseEdit extends StatefulWidget {
  ResPurchaseEdit({this.resPartner});

  final ResPartner? resPartner;

  @override
  _ResPurchaseEditState createState() => _ResPurchaseEditState();
}

class _ResPurchaseEditState extends State<ResPurchaseEdit> {
  late ResPartner resPartner;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  List<SelectOptionVO> businessTypeList = [];
  List<SelectOptionVO> industryIdList = [];
  List<SelectOptionVO> buyerIdList = [];

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.resPartner == null;
    this.resPartner = widget.resPartner ?? ResPartner();
    // if (this.isAdd == true) {
    //   this.resPartner.regTime = Utils.getDate();
    // }
    _loadSelectVO();
  }

  _loadSelectVO() async {
    businessTypeList = [
      SelectOptionVO(value: '', label: ""),
      SelectOptionVO(value: '0', label: "供应商"),
      SelectOptionVO(value: '1', label: "生产商"),
    ];
    industryIdList = await DynamicDataApi.getModelSelect(
      model: "res.partner.industry",
      name: "name",
    );
    var res = await DynamicDataApi.getModels(
      model: "hr.department",
      key: 'department_code',
      val: 'CG'
    );
    if(res.success!){
      var departmentId = res.data[0]["id"];
      buyerIdList = await DynamicDataApi.getUserSelect(val:departmentId);
    }
    setState(() {});
  }

  // selectUser() async {
  //   formKey.currentState?.save();
  //   showDialog(
  //     barrierDismissible: false,
  //     context: context,
  //     builder: (BuildContext context) => Dialog(
  //       child: UserList(),
  //     ),
  //   ).then((v) {
  //     if (v != null) {
  //       formKey.currentState!.save();
  //       resPartner.userId = v['user_id'][0].toString();
  //       resPartner.userName = v['user_id'][1].toString();
  //       setState(() {});
  //     }
  //   });
  // }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          Row(
            children: [
              Expanded(
                child: CrySelect(
                  label: '企业类型',
                  dataList: businessTypeList,
                  value: resPartner.businessType,
                  onSaved: (v) {
                    resPartner.businessType = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '供应商编码',
                  value: resPartner.ref,
                  onSaved: (v) {
                    resPartner.ref = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '用户名',
                  value: resPartner.userNameSupplier,
                  onSaved: (v) {
                    resPartner.userNameSupplier = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '供应商名称',
                  value: resPartner.name,
                  onSaved: (v) {
                    resPartner.name = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '供应商简称',
                  value: resPartner.supplierAbbreviation,
                  onSaved: (v) {
                    resPartner.supplierAbbreviation = v;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '供应商曾用名称',
                  value: resPartner.usedName,
                  onSaved: (v) {
                    resPartner.usedName = v;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '企业性质',
                  value: resPartner.person,
                  onSaved: (v) {
                    resPartner.person = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '邮编',
                  value: resPartner.zip,
                  onSaved: (v) {
                    resPartner.zip = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '电子邮件',
                  value: resPartner.email,
                  onSaved: (v) {
                    resPartner.email = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '企业电话',
                  value: resPartner.phone,
                  onSaved: (v) {
                    resPartner.phone = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '传真',
                  value: resPartner.faxes,
                  onSaved: (v) {
                    resPartner.faxes = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '公司办公地址',
                  value: resPartner.officeAddress,
                  onSaved: (v) {
                    resPartner.officeAddress = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '公司网站地址',
                  value: resPartner.website,
                  onSaved: (v) {
                    resPartner.website = v;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '生产工厂地址',
                  value: resPartner.factoryAddress,
                  onSaved: (v) {
                    resPartner.factoryAddress = v;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '开户银行',
                  value: resPartner.bank,
                  onSaved: (v) {
                    resPartner.bank = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '银行账号',
                  value: resPartner.bankAccount,
                  onSaved: (v) {
                    resPartner.bankAccount = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '公司法人姓名',
                  value: resPartner.primaryContact,
                  onSaved: (v) {
                    resPartner.primaryContact = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '法人联系方式',
                  value: resPartner.mobile,
                  onSaved: (v) {
                    resPartner.mobile = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '公司投资人姓名',
                  value: resPartner.investor,
                  onSaved: (v) {
                    resPartner.investor = v;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '各自投资比例',
                  value: resPartner.investRatio,
                  onSaved: (v) {
                    resPartner.investRatio = v;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CrySelectDate(
                  context,
                  label: '公司成立时间',
                  value: resPartner.regTime,
                  onDateChange: (v) {
                    formKey.currentState?.save();
                    resPartner.regTime = v;
                    setState(() {});
                  },
                  onSaved: (v) {
                    resPartner.regTime = v;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '税号',
                  value: resPartner.vat,
                  onSaved: (v) {
                    resPartner.vat = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInputNum(
                  label: '注册资金（万元）',
                  value: resPartner.registerMoney,
                  onSaved: (v) {
                    resPartner.registerMoney = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
              Expanded(
                child: CryInputNum(
                  label: '固定资产（万元）',
                  value: resPartner.fixedAssets,
                  onSaved: (v) {
                    resPartner.fixedAssets = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '生产规模',
                  value: resPartner.processScale,
                  onSaved: (v) {
                    resPartner.processScale = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
              Expanded(
                child: CrySelect(
                  label: '所属行业',
                  dataList: industryIdList,
                  value: resPartner.industryId,
                  onSaved: (v) {
                    resPartner.industryId = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '行业排序',
                  value: resPartner.industrySort,
                  onSaved: (v) {
                    resPartner.industrySort = v;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '银行资信证明',
                  value: resPartner.credibilityLetter,
                  onSaved: (v) {
                    resPartner.credibilityLetter = v;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: '供货区域',
                  value: resPartner.areaName,
                  onSaved: (v) {
                    resPartner.areaName = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '所属地区',
                  value: resPartner.area,
                  onSaved: (v) {
                    resPartner.area = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: CryInput(
                  label: 'QQ',
                  value: resPartner.qq,
                  onSaved: (v) {
                    resPartner.qq = v;
                  },
                ),
              ),
              Expanded(
                child: CryInput(
                  label: '信函通讯地址',
                  value: resPartner.postalAddress,
                  onSaved: (v) {
                    resPartner.postalAddress = v;
                  },
                  validator: (v) {
                    return v!.isEmpty ? S.of(context).required : null;
                  },
                ),
              ),
            ],
          ),
          CryInput(
            label: '公司营业范围',
            value: resPartner.mainBusiness,
            onSaved: (v) {
              resPartner.mainBusiness = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '产品品牌',
            value: resPartner.productBrand,
            onSaved: (v) {
              resPartner.productBrand = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '业绩简介',
            value: resPartner.performanceProfile,
            onSaved: (v) {
              resPartner.performanceProfile = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '企业简介',
            value: resPartner.enterpriseProfile,
            onSaved: (v) {
              resPartner.enterpriseProfile = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CrySelect(
            label: '评价管理人',
            dataList: buyerIdList,
            value: resPartner.buyerId,
            onSaved: (v) {
              resPartner.buyerId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            ResPartnerOdooApi.saveOrUpdateSupplier(
                    resPartner.toOdooMapSupplier())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
            // });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
